package com.gewu.pm.ui.adapter;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.hjq.base.BaseAdapter;

/**
 * 筛选页面 - 通用标签适配器（单选）
 */
public final class FilterTagAdapter extends AppAdapter<String> {

    /** 当前选中的位置 */
    private int selectedPosition = -1;

    public FilterTagAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public BaseAdapter<?>.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView tvTag;

        private ViewHolder() {
            super(R.layout.filter_time_item);
            tvTag = findViewById(R.id.tv_time);
        }

        @Override
        public void onBindView(int position) {
            String tagText = getItem(position);
            tvTag.setText(tagText);
            
            // 设置选中状态
            tvTag.setSelected(position == selectedPosition);
        }
    }

    /**
     * 设置选中位置
     * 
     * @param position 要选中的位置
     */
    public void setSelectedPosition(int position) {
        int oldPosition = selectedPosition;
        selectedPosition = position;
        
        // 刷新之前选中的item
        if (oldPosition != -1 && oldPosition < getCount()) {
            notifyItemChanged(oldPosition);
        }
        
        // 刷新当前选中的item
        if (selectedPosition != -1 && selectedPosition < getCount()) {
            notifyItemChanged(selectedPosition);
        }
    }

    /**
     * 获取当前选中的位置
     */
    public int getSelectedPosition() {
        return selectedPosition;
    }

    /**
     * 获取当前选中的文字
     */
    public String getSelectedText() {
        if (selectedPosition >= 0 && selectedPosition < getCount()) {
            return getItem(selectedPosition);
        }
        return "";
    }

    /**
     * 清除选中状态
     */
    public void clearSelection() {
        int oldPosition = selectedPosition;
        selectedPosition = -1;
        if (oldPosition != -1 && oldPosition < getCount()) {
            notifyItemChanged(oldPosition);
        }
    }
}

